/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hps.model;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;

/**
 *
 * @author $Author: NIKE3500 $
 * @version $Revision: 94 $
 * @date $Date: 2013-01-22 21:08:20 +0000 (Tue, 22 Jan 2013) $
 */

public class Barrier implements IInfluence
{

    private static Image barrierImage = null;
    private Point position;

    public Barrier(Point p)
    {
        position = p;
    }

    @Override
    public void draw(Graphics g, Point roomPosition, int singleFieldSize)
    {
        if (barrierImage == null)
        {
            barrierImage = new ImageIcon(getClass().getResource("/hps/view/HPS_icons/hindernis.png")).getImage();
        }
        g.drawImage(barrierImage,
                roomPosition.getX() + position.getX() * singleFieldSize + 1,
                roomPosition.getY() + position.getY() * singleFieldSize + 1,
                singleFieldSize, singleFieldSize, null);
    }

    @Override
    public void setInfluence(FloorField field)
    {
        //nimmt nur einen geringen Einfluss
        byte[][] byteField = field.getField();
        for (Point q : calcInfluenceArea(position))
        {
            if ((q.getX() >= 0)
                    && (q.getY() >= 0)
                    && (q.getX() < byteField.length)
                    && (q.getY() < byteField[0].length))
            {
                byteField[q.getX()][q.getY()] -= byteField[q.getX()][q.getY()] < Byte.MIN_VALUE + 10 ? Byte.MIN_VALUE - byteField[position.getX()][position.getY()] : 10;
            }
        }
    }

    @Override
    public ArrayList<IInfluence> spread(IInfluence[][] blocked)
    {
        //does not spread
        return new ArrayList<IInfluence>();
    }

    @Override
    public int getBlocked(Point p)
    {
        if (isAt(p))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    @Override
    public ArrayList<Point> getBlockedFields()
    {
        ArrayList<Point> point = new ArrayList<Point>();
        point.add(position);
        return point;
    }

    @Override
    public boolean isBlocking()
    {
        return true;
    }

    @Override
    public boolean isAt(Point pos)
    {
        if (position.equals(pos))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public IInfluence clone()
    {
        return new Barrier(position);
    }

    @Override
    public void damage(ArrayList<Pedestrian> pedestrians)
    {
        //Hindernisse verursachen keinen Schaden
    }

    private Point[] calcInfluenceArea(Point p)
    {
        Point[] rtrn = new Point[4];
        rtrn[0] = new Point(p.getX() - 1, p.getY());
        rtrn[1] = new Point(p.getX() + 1, p.getY());
        rtrn[2] = new Point(p.getX(), p.getY() - 1);
        rtrn[3] = new Point(p.getX(), p.getY() + 1);
        return rtrn;
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (o == null)
        {
            return false;
        }
        if (!(o instanceof Barrier))
        {
            return false;
        }
        if (!(o.hashCode() == this.hashCode()))
        {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 53 * hash + (this.position != null ? this.position.hashCode() : 0);
        return hash;
    }
}
